package core.physics;

import java.util.Collection;
import java.util.HashSet;

import physicsNatures.AbstractPhysicsNature;
import util.physics.Force;

/**
 * Manager class for forces which act throughout the world, e.g. gravity.
 * Get an instance of this class to use it.
 * 
 * @author Anne Weng
 * @author Nathan Klug
 *
 */
public class WorldForceManager {

    private static Collection<Force> worldForces;
    private static WorldForceManager myInstance;
    private static boolean isOn;

    private WorldForceManager() {
	worldForces = new HashSet<Force>();
	isOn = true;
    }

    /**
     * Returns an instance of WorldForceManager. 
     * @return
     */
    public static WorldForceManager getInstance() {
	if (myInstance == null) {
	    myInstance = new WorldForceManager();
	}
	return myInstance;
    }

    /**
     * Adds a force to the collection of worldwide forces.
     * @param force
     */
    public void addWorldForce(Force force) {
	worldForces.add(force);
    }

    /**
     * Removes a force from the collection of worldwide forces.
     * @param force
     */
    public void removeWorldForce(Force force) {
	worldForces.remove(force);
    }

    /**
     * Applies all of the worldwide forces to something with a physics nature.
     * @param physics
     * @param elapsedTime
     */
    public void applyWorldForces(AbstractPhysicsNature physics, long elapsedTime) {
	if (isOn) {
	    for (Force force : worldForces) {
		physics.applyForce(force, elapsedTime);
	    }
	}
    }

    /**
     * Returns a boolean value representing if physics is on
     * @return
     */
    public boolean isOn(){
        return isOn;
    }
    
    /**
     * Turns physics off
     */
    public void turnOff(){
        isOn = false;
    }
    
    /**
     * Turns physics on
     */
    public void turnOn(){
        isOn = true;
    }
    
    /**
     * Sets the physics on/off based on the input boolean
     * @param newState
     */
    public void setPhysicsOnOff(boolean newState){
        isOn = newState;
    }
    
    /**
     * Turns physics on if it is off and turns physics off if it is on
     */
    public void togglePhysicsOnOff(){
        isOn = !isOn;
    }
}
